package com.changanw.tienchin.assignment.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changanw.tienchin.assignment.domain.Assignment;
import com.changanw.tienchin.assignment.mapper.AssignmentMapper;
import com.changanw.tienchin.assignment.service.IAssignmentService;
import com.changanw.tienchin.common.constant.TienChinConstants;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author changanw
 * @since 2023-11-25
 */
@Service
public class AssignmentServiceImpl extends ServiceImpl<AssignmentMapper, Assignment> implements IAssignmentService {

  @Override
  @Transactional
  public AjaxResult assignClue(Assignment assignment) {


    //1. 将之前已经分配的线索 的 latest全部改为false
    UpdateWrapper<Assignment> uw = new UpdateWrapper<>();
    uw.lambda().set(Assignment::getLatest,false).eq(Assignment::getAssignId,assignment.getAssignId());
    update(uw);
    //2. 再去分配线索
    //此处类型由前端传递，0：线索 1：商机
    //assignment.setType(TienChinConstants.CLUE_TYPE);
    assignment.setCreateBy(SecurityUtils.getUsername());
    assignment.setCreateTime(LocalDateTime.now());
    assignment.setLatest(true);
    save(assignment);
    return AjaxResult.success("分配成功");

  }
}
